import { message as Message } from "ant-design-vue";
import { computed, Ref } from "vue";
export default function ShowModalHook(state: { dialogOpts: Base.DialogOpts; drawerOpts: Base.DrawerOpts; tableRef: Ref<any>; upLoadType: Ref<string>; upLoadTemplate: Ref<number> }) {
  const { dialogOpts, drawerOpts, tableRef, upLoadType, upLoadTemplate } = state;
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  const showHandle = (row: ClientWaybillManagement.ListRows) => {
    drawerOpts.title = `查看(${row.waybillNum})`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "Details";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };
  // 更改渠道
  const modifyChannelHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    dialogOpts.title = `修改渠道`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyChannel";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };

  // 更改代理渠道
  const modifyAgentChannelHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    dialogOpts.title = `更改代理渠道`;
    dialogOpts.width = "520px";
    dialogOpts.component = "ModifyAgentChannel";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };

  // 修改品名
  const modifyProductNameHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    if (selectRows.value[0].makeInvoiceStatus === -2) return Message.error("请修改运单发票内材质！");
    dialogOpts.title = `修改品名`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyProductName";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };
  // 修改材质
  const modifyMasterialHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    if (selectRows.value[0].makeInvoiceStatus === -2) return Message.error("请修改运单发票内材质！");
    dialogOpts.title = `修改材质`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyMasterial";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };

  // 批量修改转单号
  const transferNumberHandle = () => {
    dialogOpts.title = `批量修改转单号`;
    dialogOpts.width = "1000px";
    dialogOpts.component = "TransferNumber";
    dialogOpts.visible = true;
  };

  // 设置转单号
  const setTransferNumberHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    dialogOpts.title = `设置转单号`;
    dialogOpts.width = "520px";
    dialogOpts.component = "SetTransferNumber";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };

  // 操作标识
  const modifyMarkHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    dialogOpts.title = `标识`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "Mark";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };

  // 创建工单
  const createWorkOrderHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `创建工单`;
    dialogOpts.width = "520px";
    dialogOpts.component = "CreateWorkOrder";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  // 回退
  const returnOrderHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `回退`;
    dialogOpts.width = "520px";
    dialogOpts.component = "ReturnOrder";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  //配置代理
  const agentHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `配置代理`;
    dialogOpts.width = "520px";
    dialogOpts.component = "OutOfWarehouse";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  // 查看统计
  const showStatictisHandle = () => {
    dialogOpts.title = "查看统计";
    dialogOpts.width = "900px";
    dialogOpts.component = "DataStatistics";
    dialogOpts.visible = true;
  };

  //制作发票
  const makeInvoiceHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选1条运单!");
    if (selectRows.value[0].makeInvoiceStatus === -2) return Message.error("只能操作未制作发票的数据!");
    drawerOpts.title = `制作发票`;
    drawerOpts.width = "1800px";
    drawerOpts.type = 1;
    drawerOpts.component = "MakeInvoice";
    drawerOpts.passData = selectRows.value[0];
    drawerOpts.visible = true;
  };

  //日志
  const showLogHandle = (row: WarehouseEntryChannel.ListRows) => {
    drawerOpts.title = "日志";
    drawerOpts.width = "1200px";
    drawerOpts.passData = row;
    drawerOpts.component = "OperationLog";
    drawerOpts.type = 2;
    drawerOpts.visible = true;
  };

  // 代理状态标记
  const agentStatusSignHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `代理状态标记`;
    dialogOpts.width = "520px";
    dialogOpts.component = "AgentStatusSign";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  // 修改材积明细
  const modifyVolumeDetailsHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选一条运单!");
    drawerOpts.title = `修改材积明细`;
    drawerOpts.width = "1200px";
    drawerOpts.component = "ModifyVolumeDetails";
    drawerOpts.type = 1;
    drawerOpts.passData = selectRows.value;
    drawerOpts.visible = true;
  };

  // 同步材积明细
  const updateBoxVolumeHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选一条运单!");
    drawerOpts.title = `同步材积明细`;
    drawerOpts.width = "1200px";
    drawerOpts.component = "ModifyVolumeDetails";
    drawerOpts.type = 2;
    drawerOpts.passData = selectRows.value;
    drawerOpts.visible = true;
  };

  // 修改结算重
  const modifySettlementDuplicateHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("只能勾选一条运单!");
    dialogOpts.title = `修改结算重`;
    dialogOpts.width = "520px";
    dialogOpts.component = "ModifySettlementDuplicate";
    dialogOpts.passData = selectRows.value[0];
    dialogOpts.visible = true;
  };
  //转运
  const isTransferBillHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `转运`;
    dialogOpts.width = "520px";
    dialogOpts.component = "isTransferBill";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  //问题件
  const addProblemShipment = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    if (selectRows.value.length > 1) return Message.error("一次勾选最多1条!");
    if (selectRows.value[0].status === 0) return Message.error("取消的运单不可发起问题件!");
    drawerOpts.title = `发起问题件`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "ProblemShipment";
    drawerOpts.passData = selectRows.value[0];
    drawerOpts.visible = true;
  };
  //导入问题件
  const importProblemHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    dialogOpts.title = `导入问题件`;
    dialogOpts.width = "500px";
    dialogOpts.component = "UploadfileAndDownLoadTemplate";
    dialogOpts.visible = true;
    upLoadType.value = "Problem";
    upLoadTemplate.value = 13;
  };
  return {
    showHandle,
    modifyChannelHandle,
    modifyMasterialHandle,
    modifyProductNameHandle,
    modifyMarkHandle,
    transferNumberHandle,
    setTransferNumberHandle,
    returnOrderHandle,
    showStatictisHandle,
    makeInvoiceHandle,
    modifyAgentChannelHandle,
    showLogHandle,
    agentStatusSignHandle,
    modifyVolumeDetailsHandle,
    modifySettlementDuplicateHandle,
    agentHandle,
    createWorkOrderHandle,
    isTransferBillHandle,
    updateBoxVolumeHandle,
    addProblemShipment,
    importProblemHandle
  };
}
